package duaxe.home.level;

import java.util.ArrayList;
import java.util.List;

import duaxe.home.Graphics.Screen;
import duaxe.home.entity.Entity;
import duaxe.home.entity.projectile.Projectile;
import duaxe.home.level.tile.Tile;

public class Level {
	//protected Tile[] tiles;
	protected int width, height;
	protected int[] tilesInt;
	protected int[] tiles;
	protected int tile_size;
	
	protected List<Entity> entities = new ArrayList<Entity>();
	private List<Projectile> projectiles = new ArrayList<Projectile>();
	
	public static Level spawn = new SpawnLevel("/textures/levels/spawn.png");
	

	public Level(int width, int height) {
		this.width = width;
		this.height = height;
		tilesInt = new int[width * height];
		generateLevel();

	}

	public Level(String path) {
		loadLevel(path);
		generateLevel();
	}

	protected void generateLevel() {
		for(int y =0; y<64; y++){
			for(int x=0; x<64; x++){
				getTile(x, y);
			}
		}
		tile_size = 16;
	}

	protected void loadLevel(String path) {
	}

	public void update() {
		for(int i=0; i<entities.size(); i++){
			entities.get(i).update();;	
		}
		for(int i=0; i<projectiles.size(); i++){
			projectiles.get(i).update();	
		}
		
	}

	private void time() {
	}
	public boolean tilecollision(double x, double y,double xa,double ya, double size) {
		boolean solid = false;
		for (int c = 0; c < 4; c++) {
			double xt = ((x + xa) + c / 2 * size*2-10) / 16;
			double yt = ((y + ya) + c / 2 * size/8-2) / 16;
			if (getTile((int)xt, (int)yt).solid()) {
				solid = true;
			}
		}
		return solid;
	}
	public List<Projectile> getProjectiles() {
		return projectiles;
	}
	

	public void render(int xScroll, int yScroll, Screen screen) {
		screen.setOffset(xScroll, yScroll);
		int x0 = xScroll >> 4;
		int x1 = (xScroll + screen.width+16) >> 4;
		int y0 = yScroll >> 4;
		int y1 = (yScroll + screen.height+16) >> 4;

		for (int y = y0; y < y1; y++) {
			for (int x = x0; x < x1; x++) {
				getTile(x, y).render(x, y, screen);
//				if (x+y*16<0 || x+y*16 >= 16*16 ){
//				Tile.voidTile.render(x, y, screen);
//				continue;
//				}
			//	else
					//return Tile.voidTile;
				//tiles[x+y*16].render(x, y, screen);
			}
		}
		for(int i=0; i<entities.size(); i++){
			entities.get(i).render(screen);	
		}
		for(int i=0; i<projectiles.size(); i++){
			projectiles.get(i).render(screen);	
		}
	}
	public void add(Entity e){
		entities.add(e);
		
	}
	public void addProjectile(Projectile p){
		projectiles.add(p);
		p.init(this);
		
	}

	public Tile getTile(int x, int y) {
		if (x < 0 || y < 0 || x >= width || y >= height) return Tile.voidTile;
		if (tiles[x + y * width] == Tile.col_spawn_floor)return Tile.spawn_floor;
		if (tiles[x + y * width] == Tile.col_spawn_grass)return Tile.spawn_grass;
		if (tiles[x + y * width] == Tile.col_spawn_wall1){
//			System.out.println(x+"-"+y);
			return Tile.spawn_wall1;
		}
		if (tiles[x + y * width] == Tile.col_spawn_wall2)return Tile.spawn_wall2;
		
		return Tile.voidTile;
	}

}
